/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upc.clase.demo.dao.impl;

import edu.upc.clase.demo.dao.CanchaDao;
import edu.upc.clase.demo.entity.Cancha;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Hariki
 */

@Repository
public class CanchaDaoImpl extends SimpleJdbcDaoSupport implements CanchaDao {

    private static Logger log = LoggerFactory.getLogger(CanchaDaoImpl.class);

    @Autowired
    public CanchaDaoImpl(DataSource dataSource) {
        log.info("Asignando el dataSource");
        setDataSource(dataSource);
    }

    @Override
    public Integer insertar(Cancha cancha) {

        getJdbcTemplate().update(
                "insert into cancha (NoCancha,CoLocal,CoTipoCancha,Qtlargo,Qtancho,CoPersona,FeRegistro,FlEstado) values (?, ?, ?, ?, ?, ?, getdate(), ?)",
                cancha.getNoCancha(),cancha.getCoLocal(),cancha.getCoTipoCancha(),cancha.getQtlargo(),cancha.getQtAncho(),cancha.getCoPersona(),cancha.getFlEstado());
        return getSimpleJdbcTemplate().queryForInt("call identity()");
    }

    @Override
    public void actualizar(Cancha cancha) {
        getJdbcTemplate().update(
                "update cancha set nocancha = ?, colocal = ?, cotipocancha= ?, qtlargo= ?, qtancho= ?, copersona= ?, feregistro=getdate(),flestado= ? where cocancha = ?",
                cancha.getNoCancha(),cancha.getCoLocal(),cancha.getCoTipoCancha(),cancha.getQtlargo(),cancha.getQtAncho(),cancha.getCoPersona(),cancha.getFlEstado(),cancha.getCoCancha());
    }

    @Override
    public void eliminar(Cancha cancha) {
        getJdbcTemplate().update(
                "delete from cancha where CoCancha = ?", cancha.getCoCancha());
    }

    @Override
    public Cancha buscar(Integer CoCancha) {
        try {
            return getSimpleJdbcTemplate().queryForObject(
                    "select * from cancha where CoCancha=?",
                    new BeanPropertyRowMapper<Cancha>(Cancha.class),CoCancha);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public List<Cancha> buscarTodos() {
        return getSimpleJdbcTemplate().query(
                "select * from cancha",
                new BeanPropertyRowMapper<Cancha>(Cancha.class));
    }


    @Override
    public List<Cancha> buscarPorNombre(String nocancha) {
        try {
            Map<String,String> parametros = new HashMap<String,String>();
            parametros.put("nocancha","%"+nocancha+"%");
            return getSimpleJdbcTemplate().query(
                    "select * from cancha where cancha like :nocancha",
                    new BeanPropertyRowMapper<Cancha>(Cancha.class),parametros);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
}
